package com.oceansoft.mobile.biz.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.biz.api.entity.Member;
import com.oceansoft.mobile.common.Constant;
import com.oceansoft.mobile.common.util.HttpClientUtil;
import com.oceansoft.mobile.common.util.Result;
import com.oceansoft.mobile.common.util.StringUtils;
import com.oceansoft.mobile.weixin.util.RequestUtils;
import com.oceansoft.mobile.weixin.util.WeixinUtil;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Created by zhuya on 2016/3/3.
 */
@Controller
@RequestMapping("api/lawyer")
public class ApiLawyerController {
    //告知办案单位
    public static String  JudgePer=Constant.host5+"api/LSWSYYHJ/JudgePer";

    //网上预约
    public static String LSWSYYHJ =Constant.host5+"api/LSWSYYHJ/LSWSYYHJ";
    //现场预约
    public static String LSXCYYHJ =Constant.host5+"api/LSWSYYHJ/LSXCYYHJ";


    //介绍信
    public static String cltxz ="52f9912d-4392-4325-baf6-03e2881291d5";
    //委托公函
    public static String gdgcjh ="b5912048-c494-4d3c-b428-24e802de9ccd";
    //律师证
    public static String qtcl ="5df2077b-725c-4635-82d5-bf063e236e5e";
    //律师事项ID
    public static String lawyer_code ="80d4c89e-69af-4978-aa1c-1d8c63d7f2e1";
    //律师事项部门ID
    public static String orgId ="e5b8381e-0e43-4683-8ec7-f9be9764d60c";
    //获取事项详情
    public static String getcase =Constant.host5+"api/getcase/GetDetail";

    /*
            * 告知办案单位
            *
            * @param model
            * @return
            * @throws Exception
            */
    @ResponseBody
    @RequestMapping(value = "JudgePer")
    public Result JudgePer(HttpSession session,HttpServletRequest request,
                           Model model)throws IllegalArgumentException, IOException {
        JSONObject param =  RequestUtils.buildParamsByNamesFromRequest4JSON(request);
        Result result =   RequestUtils.saveSpecific(param, JudgePer);
        session.setAttribute(Constant.SESSION_LAWYER_JudgePer,result.getData());
        return result;
    }
    /*
       * 网上预约
       *
       * @param model
       * @return
       * @throws Exception
       */
    @ResponseBody
    @RequestMapping(value = "LSWSYYHJ")
    public Result submitlawyer1(HttpSession session,HttpServletRequest request,
                           Model model)throws IllegalArgumentException, IOException {
        String LAWYERGUID = (String)session.getAttribute(Constant.SESSION_LAWYER_JudgePer);
        JSONObject param =  RequestUtils.buildParamsByNamesFromRequest4JSON(request);
        param.put("LAWYERGUID",LAWYERGUID);
        Result result =  RequestUtils.saveSpecific(param, LSWSYYHJ);
        Result rst = new Result();
        if(result.isSucc()){
            param.clear();
            param.put("declareCaseGuid",lawyer_code);
            String projectGuid = result.getData().toString();
            rst =  RequestUtils.submitShenbao(param, request, projectGuid, Constant.SESSION_LAWYER_FJ, true);
            return rst;
        }
        return result;
    }

    /*
        * 现场预约
        *
                * @param model
        * @return
                * @throws Exception
        */
    @ResponseBody
    @RequestMapping(value = "LSXCYYHJ")
    public Result submitlawyer2(HttpSession session,HttpServletRequest request,
                           Model model)throws IllegalArgumentException, IOException {
        Map<String, String> param = new HashMap<String, String>();
        param=RequestUtils.buildParamsByNamesFromRequest(request);
        Result result = JSONObject.parseObject((HttpClientUtil.doPost(LSXCYYHJ, param, "utf-8")), Result.class);
        Result rst = new Result();
        //session.setAttribute(Constant.SESSION_LAWYER_LSXCYYHJ,param);
        if(result.isSucc()){
            String url = Constant.host + "/api/matters/shenBao";
            String projectGuid = result.getData().toString();
            //附件Id
            Map<String, String> m = new HashMap<String, String>();
            session.removeAttribute(Constant.SESSION_LAWYER_JudgePer);
            session.removeAttribute(Constant.SESSION_LAWYER_FJ);
            m.put("declareCaseGuid", lawyer_code);
            m.put("projectGuid",projectGuid);
            //附加用户信息
            RequestUtils.putUser(request, m);
            rst = JSONObject.parseObject(HttpClientUtil.doPost(url, m, "utf-8"), Result.class);
            return rst;
        }
        return result;
    }
    /**
     *施工车辆图片上传2
     * @param base64
     * @return
     */
    @ResponseBody
    @RequestMapping(value="uploadMsg",method= RequestMethod.POST)
    public Result uploadMsg(HttpServletRequest request){
    	String type=request.getParameter("type");
		String seesionname = request.getParameter("imgname");
		String base64 = request.getParameter("base64");
		String name=request.getParameter("name");
    	//type字段用于识别前台传过来的不同图片
        String caseMatGuid = "";
        if("1".equals(type)){
            caseMatGuid=qtcl;//,
        }else if("2".equals(type)){
            caseMatGuid=cltxz;//gdgcjh,
        }else if("3".equals(type)){
            caseMatGuid=gdgcjh;//gdgcjh,qtcl
        }
        Result result = RequestUtils.saveFj(request, base64, caseMatGuid, true);
        Map<String ,Result> m = (Map<String , Result>)request.getSession().getAttribute(Constant.SESSION_LAWYER_FJ);
        request.getSession().removeAttribute(Constant.SESSION_LAWYER_FJ);
        if(null==m)
        {
            m = new HashMap<String ,Result>();
        }
        m.put(seesionname, result);
        request.getSession().setAttribute(Constant.SESSION_LAWYER_FJ, m);
        return result;
    }
    

}
